#pragma once

#include "LogRecord.h"

void LogRecord::WriteLog(wchar_t *format ...) {
	
	va_list args;
	va_start(args, format);
	int bufferLen = vwprintf_s(format, args) + 1;
	wchar_t *logMsg = new wchar_t[bufferLen]{0};
	vswprintf_s(logMsg, bufferLen, format, args);
	va_end(args);
	if (logMsg[bufferLen - 2] != L'\n')
		logMsg[bufferLen - 1] = L'\n';
	
	HANDLE hLogFile = CreateFileW(L"logs.txt", GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
	if (hLogFile == INVALID_HANDLE_VALUE) {
		return;
	}
	SetFilePointer(hLogFile, 0, NULL, FILE_END);
	DWORD buffWritenLen = 0;
	WriteFile(hLogFile, logMsg, bufferLen * sizeof(wchar_t), &buffWritenLen, NULL);
	CloseHandle(hLogFile);
	
}